/**
 * @author LKQ
 * @date 2022/3/1 14:24
 * @description
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.makeStringSorted("cba"));
    }
    public int makeStringSorted(String s) {
        int MOD = 1_000_000_007;
        int cnt = 0, n = s.length();
        char[] c = s.toCharArray();
        while (!isSorted(c)) {
            int p = 0, q = 0;
            for (int i = 1; i < n; i++) {
                if (c[i-1] > c[i]) {
                    p = i;
                }
            }
            for (int j = p; j  < n; j++) {
                boolean fit = true;
                for (int k = p; k <= j; k++) {
                    if (c[k] >= c[p - 1]) {
                        fit = false;
                        break;
                    }
                }
                if (!fit) {
                    continue;
                }
                q = Math.max(q, j);
            }
            swap(c, p-1, q);
            reverse(c, p);
            cnt++;
            cnt = cnt % MOD;
        }
        return cnt;
    }

    public void swap(char[] chars, int i, int j) {
        char temp = chars[i];
        chars[i] = chars[j];
        chars[j] = temp;
    }
    public void reverse(char[] chars, int start) {
        int left = start, right = chars.length-1;
        while (left < right) {
            swap(chars, left, right);
            left++;
            right--;
        }
    }
    public boolean isSorted(char[] chars) {
        for (int i = 1; i < chars.length; i++) {
            if (chars[i] < chars[i-1]) {
                return false;
            }
        }
        return true;
    }
}
